Is It Legal?
How should programs respond to illegal data? Assuming that data will be legal asks for trouble: What if users mistype? What if users want to confuse the program? This case study describes how to guard against illegal data in the Check That Number!, Banners With CLASS, Calendar Shop, and Roman Calculator Construction programs.
Problem statement
The problem statements for Check That Number!, Banners With CLASS, The Calendar Shop, and Roman Calculator Construction all make assumptions about the nature of the input. Such assumptions are rarely justified. What if users mistype? What if the user tries to confuse the program? A better approach is to make sure that only legal input is processed. This case study explores general ways to check input for errors as well as specific solutions for the programs encountered up to now.
Modify the programs for Check That Number!, Banners With CLASS, The Calendar Shop, and Roman Calculator Construction so that they handle invalid input appropriately. They should prompt for input, read a value or values from the user without crashing, analyze the values, and repeat this process until the user provides legal input.
Recall the legal input for the four earlier case studies:
Check That Number! | a line containing only a four-digit identification number |
Banners With CLASS | a line containing a legal letter size (an integer between 5 and 24, inclusive), possibly preceded by blanks; a line containing exactly six characters, all chosen from P, Y, T, H, O and N |
The Calendar Shop | a line containing a legal year, possibly preceded by blanks |
Roman Calculator Construction | a line containing a legal Roman numeral |
Analysis
5.1 Why was it initially reasonable to assume that the data for the Check That Number! and Roman Calculator Construction programs would be legal? Why would this assumption not be appropriate for the Banners With CLASS or Calendar Shop programs?
Reflection
5.2 How susceptible are you to making typing errors? If you are an inaccurate typist, how do you catch errors?
Reflection
5.3 Which programs, when you run them, have left you unsure about what to type at a given point? What improvements that would help might be incorporated in these programs?
Testing
5.4 Think of ways to crash the programs produced in earlier case studies. Catalog the error messages or crash situations that result.